$(document).ready(function() {
	$( ".login-container" ).addClass("ui-widget");
	$( ".data-div input" ).addClass("text ui-widget-content ui-corner-all prompt");
	$( ".data-div label span" ).css("color","#f2422b");
	$( ".data-div" ).hide().show("fade",1000);
	$( ".login-container h3, .login-container h4, .button" ).hide().show("fade",1000);
	$( ".button" ).button();
	$( ".data-div input" ).focus(function(){
		var value = $(this).val();
		if((value==="Enter password")){
			$(this).attr("type","password");	
		};
		if($(this).hasClass("prompt")){
			$(this).val("");
			$(this).removeClass("prompt");
			$(this).blur(function(){
				if($(this).val()===""){
					$(this).attr("type","text");
					$(this).val(value);
					$(this).addClass("prompt");
				}
			});
		}
	});
	var email = $( "input[name='email']" ),
	pass = $( "input[name='pass']" ),
	lastname,
	allFields = $( [] ).add( email ).add( pass ),
	error = $( ".login-container .error-message" ).css("color","#f2422b"),
	emailRegex = /^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i;
	function updateErrorMessage( m ) {
		error
		.hide()
		.text( m )
		.show("pulsate",500);
		setTimeout(function() {
			error.removeClass( "ui-state-highlight", 1500 );
		}, 500 );
	}
	function checkRegexp( o, regexp, m ) {
		if (!(regexp.test(o.val()))){
			o.addClass( "ui-state-error" );
			updateErrorMessage( m );
			return false;
		} else {
			o.removeClass("ui-state-error");
			error.hide();
			return true;
		}
	}
	function validateFields(){
		allFields.removeClass("ui-state-error");
		var valid = true;
		if(email.hasClass("prompt")||pass.hasClass("prompt")){
			updateErrorMessage("Email and password are required");
			valid = false;
		}else{
			valid = valid && checkRegexp( email,emailRegex, 
										  "Write an email with the format someone@example.com" );
			if(valid){
				var valid_email=false;
				var valid_password=false;
				$.getJSON('assets/data/investor.json', function(data){
				    $.each(data.investors, function(index, investor){
						if(investor.email===email.val()){
							valid_email = true;
							if(investor.password===pass.val()){
								valid_password = true;
								lastname=investor.lastname;
							}
						}
				    });
				}).done(function(){
					if(!valid_email){
						updateErrorMessage("The entered email address does not exist, please enter other email or register as an investor.");
						valid=false;
					}else if(!valid_password){
						updateErrorMessage("The password is incorrect. Be sure to use the password for your AgroPortfolio account");
						valid=false;
					}else{
						valid=true;
						alert("You are logged as "+lastname+"!");
						email.val("").trigger("blur");
						pass.val("").trigger("blur");
					}
				});
			}
		}
	}
	$( ".button" ).click(function(){
		validateFields();
		adjustHeight();
	});
});